package com.xian.gaoxiao.db;

import com.xian.gaoxiao.db.CommentTable.CommentColumns;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;

public class TaskItemTable implements AppDBConstants {

	public static final String TAG = "TaskItemTable";
	public static final String TABLE_NAME = Tables.TASKITEM;

	private SQLiteDatabase db;

	public TaskItemTable(SQLiteDatabase db) {
		this.db = db;
	}

	public static class TaskItemColumns implements BaseColumns {
		public static final String ID = "_id";
		public static final String CREATEDATE = "createdate";
		public static final String ALERTDATE = "alertdate";
		public static final String ENABLEALERT = "enablealert"; // 0不可用 1可用
		public static final String BEGINDATE = "begindate";
		public static final String FINISHDATE = "finishdate";
		public static final String MODIFYDATE = "modifydate";
		public static final String STATUS = "status"; // default 0 ， 0未完成 1已完成
														// 2正在进行 3暂停 4取消
		public static final String TITLE = "title";
		public static final String RANK = "rank";
		public static final String SORT = "sort"; // default 0
		public static final String DATA = "data";
		public static final String PIC = "pic";
		public static final String SOUND = "sound";
	}

	public int insert(Long alertdate, Integer enablealert, Long begindate,
			Long finishdate, String title, Integer rank, String data,
			byte[] pic, String sound) {
		int result = 0;
		try {
			StringBuilder builder = new StringBuilder();
			builder.append("INSERT INTO ");
			builder.append(TABLE_NAME);
			builder.append(" ( ");
			builder.append(TaskItemColumns.CREATEDATE);
			builder.append(" ,");
			builder.append(TaskItemColumns.ALERTDATE);
			builder.append(" ,");
			builder.append(TaskItemColumns.ENABLEALERT);
			builder.append(" ,");
			builder.append(TaskItemColumns.BEGINDATE);
			builder.append(" ,");
			builder.append(TaskItemColumns.FINISHDATE);
			builder.append(" ,");
			builder.append(TaskItemColumns.TITLE);
			builder.append(" ,");
			builder.append(TaskItemColumns.RANK);
			builder.append(" ,");
			builder.append(TaskItemColumns.DATA);
			builder.append(" ,");
			builder.append(TaskItemColumns.PIC);
			builder.append(" ,");
			builder.append(TaskItemColumns.SOUND);
			builder.append(" ) VALUES (");
			builder.append(System.currentTimeMillis());
			builder.append(" , ");
			builder.append(alertdate);
			builder.append(" , ");
			builder.append(enablealert);
			builder.append(" , ");
			builder.append(begindate);
			builder.append(" , ");
			builder.append(finishdate);
			builder.append(" , '");
			builder.append(title);
			builder.append("' , ");
			builder.append(rank);
			builder.append(" , '");
			builder.append(data);
			builder.append("' , ");
			builder.append(pic);
			builder.append(" , '");
			builder.append(sound);
			builder.append("' )");
			Log.e(TAG, builder.toString());
			db.execSQL(builder.toString());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public int delete(Integer id) {
		int result = 0;
		try {
			result = db.delete(TABLE_NAME, TaskItemColumns.ID + " = " + id,
					null);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public int update(ContentValues values, String whereClause,
			String[] whereArgs) {
		int result = 0;
		try {
			values.put(CommentColumns.MODIFYDATE, System.currentTimeMillis());
			result = db.update(TABLE_NAME, values, whereClause, whereArgs);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public Cursor query(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		Cursor cursor = null;
		try {
			cursor = db.query(TABLE_NAME, columns, selection, selectionArgs,
					null, null, orderBy, limit);
			return cursor;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
